package com.liulichev.omstest;

import java.io.File;
import java.io.IOException;

import org.mapsforge.android.maps.MapActivity;
import org.mapsforge.android.maps.MapView;
import org.mapsforge.android.maps.mapgenerator.MapGeneratorFactory;
import org.mapsforge.android.maps.mapgenerator.MapGeneratorInternal;
import org.mapsforge.android.maps.rendertheme.InternalRenderTheme;
import org.mapsforge.core.GeoPoint;

import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Toast;

public class HelloMapView extends MapActivity {
	private MapView mapView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);
		this.mapView = (MapView) findViewById(R.id.mapView);
		mapView.setClickable(true);
		mapView.setBuiltInZoomControls(true);
		
		try {
			mapView.setMapFile(getMapFilePath());
		} catch (IOException e) {
			Toast.makeText(getApplicationContext(), "Cannot read map file", Toast.LENGTH_LONG).show();
		}
		mapView.setCenter(new GeoPoint(50.444183, 30.610034));
		mapView.getController().setZoom(12);
		mapView.setMapGenerator(MapGeneratorFactory
				.createMapGenerator(MapGeneratorInternal.DATABASE_RENDERER));

		float moveSpeedFactor = (float) 30;

		mapView.getMapMover().setMoveSpeedFactor(moveSpeedFactor);
		mapView.getFileSystemTileCache().setPersistent(true);
		mapView.getFileSystemTileCache().setCapacity(500);
		mapView.setPersistentDrawingCache(ViewGroup.PERSISTENT_ALL_CACHES);
		this.mapView.setRenderTheme(InternalRenderTheme.OSMARENDER);
		// mapView.
		 //mapView.setDebugSettings( new DebugSettings(false, false, false));
	}
	
	public String getMapFilePath() throws IOException{
		String destinationFolder = Environment.getExternalStorageDirectory().getAbsolutePath() + "/map/";
		File destinationFile = new File(destinationFolder + "kiev.map");
		if(destinationFile.exists()){
			return destinationFile.getAbsolutePath();
		}
		
		File destinationDir = new File(destinationFolder);
		
		if(!destinationDir.exists()){
			destinationDir.mkdir();
	    }
		
		return FileUtils.extractZippedMapFile(getAssets().open("kiev.zip"), destinationFolder);
	}

	@Override
	protected void onResume() {
		super.onResume();

	}
}